<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserType;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserTypeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserType(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');

            // 也可以这样
            $grid->disableBatchDelete();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserType(), function (Show $show) {
            $show->field('id');
            $show->field('name');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserType(), function (Form $form) {
            $form->display('id');
            $form->text('name');

            $form->deleted(function (Form $form,$result){
               //获取待删除数据，这里获取的是一个数组
                $data = $form->model()->toArray();
//                dd($data);
                if (!$result){
                    return $form->error("数据删除失败");
                }
                User::where("cate_type",$data[0]['id'])->update(['cate_type'=>null]);
                // 返回删除成功提醒，此处跳转参数无效
//                return $form->success('删除成功');
            });
        });
    }
}
